//
//  Pet_ShareViewController.swift
//  PetApp
//
//  Created by mac on 2023/9/18.
//  Copyright © 2023 Pet_. All rights reserved.
//

import UIKit

class Pet_CardViewController: BasicViewController,NaviBarHidden {
    
    let addB = UIButton.initV(img: "icon_post")
    let topI = UIImageView.initImgView(img: "titttle_my pet")
    let backV = UIImageView.initImgView(img: "piture_background")
    lazy var btn1: itemButton = {
        let b = itemButton.init(frame: .zero)
        b.nameString = "Dog"
        return b
    }()
    lazy var btn2: itemButton = {
        let b = itemButton.init(frame: .zero)
        b.nameString = "Cat"
        return b
    }()
    lazy var btn3: itemButton = {
        let b = itemButton.init(frame: .zero)
        b.nameString = "Bird"
        return b
    }()
    lazy var btn4: itemButton = {
        let b = itemButton.init(frame: .zero)
        b.nameString = "Category"
        return b
    }()
    var selectType = "Dog"
    
    private lazy var cardListView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 10.Fit
        layout.minimumInteritemSpacing = 10.Fit
        layout.scrollDirection = .horizontal
        let h = screenHeight - (status_bar_height() + 320 + TabbarHeight)
        layout.itemSize = .init(width: 220.Fit, height: h)
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.register(CardList_Item.self, forCellWithReuseIdentifier: CardList_Item.description())
        view.contentInset = .init(top:0, left: 15.Fit, bottom: 0, right: 15.Fit)
        view.backgroundColor = .clear
        view.delegate = self
        view.dataSource = self
        view.bounces = false
        return view
    }()
    var dataSource:[InfoUtil.room]? = []

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.getData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.subviews {
            topI
            addB
            backV.subviews {
                btn1
                btn2
                btn3
                btn4
                cardListView
            }
        }
        
        topI.width(screenWidth).height(60.Fit).top(status_bar_height() + 15.Fit)
        topI.centerHorizontally()
        
        addB.Top == topI.Bottom - 5.Fit
        addB.size(50.Fit).right(15.Fit)
        addB.addAct {
            let vc = Pet_PublishCardViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
        
        backV.left(0).right(0).width(375.Fit).height(751.Fit)
        backV.Top == addB.Bottom
        backV.isUserInteractionEnabled = true
        
        btn1.left(0).top(50.Fit).width(screenWidth/4).height(80.Fit)
        btn2.left(screenWidth/4).top(30.Fit).width(screenWidth/4).height(80.Fit)
        btn3.left(screenWidth/4*2).top(30.Fit).width(screenWidth/4).height(80.Fit)
        btn4.left(screenWidth/4*3).top(50.Fit).width(screenWidth/4).height(80.Fit)

        let h = screenHeight - (status_bar_height() + 320 + TabbarHeight)
        cardListView.left(0).right(0).height(h)
        cardListView.Top == btn4.Bottom + 30.Fit
        
        self.selectIndexPage(index: 0)
        
        btn1.addAct {
            self.selectIndexPage(index: 0)
        }
        btn2.addAct {
            self.selectIndexPage(index: 1)
        }
        btn3.addAct {
            self.selectIndexPage(index: 2)
        }
        btn4.addAct {
            let view1 = Card_ListView()
            view1.doneBlock = { name,index in
                if index < 3{
                    self.btn1.nameString = "Dog"
                    self.btn2.nameString = "Cat"
                    self.btn3.nameString = "Bird"
                    self.selectIndexPage(index: index)
                }else{
                    self.btn1.nameString = "Rabbit"
                    self.btn2.nameString = "Fish"
                    self.btn3.nameString = "Others"
                    self.selectIndexPage(index: index - 3)
                }
            }
            var h = screenHeight - NavHeight - 250.Fit - TabbarHeight
            if UIDevice.modelName.contains("iPad") || UIDevice.modelName == "iPhone SE"{
                h = screenHeight - NavHeight - 280.Fit - TabbarHeight
            }
            let page = Pop_Up_Custom_View(config: PopUpViewConfig(view: view1, size: CGSize(width: 180.Fit, height:h)),top: NavHeight + 230.Fit,right: 35.Fit)
            page.isCloseFromClean = true
            page.animatedPop()
        }
        self.cardListView.emptyGameView(.noList)
    }
        
   @objc func selectIndexPage(index:Int) -> Void {
        btn1.selectButton(on: false)
        btn2.selectButton(on: false)
        btn3.selectButton(on: false)
        let arr:[itemButton] = [btn1,btn2,btn3]
        arr[index].selectButton(on: true)
        selectType = arr[index].nameString
        self.getData()
    }
    
    func getData() -> Void {
        Toast_View_Tool.show()
        RT.getRoomList { list in
            self.dataSource?.removeAll()
            for item in list.lists{
                if item.info3 == "0" && item.info0 == self.selectType{
                    self.dataSource?.append(item)
                }
            }
            Toast_View_Tool.dismiss()
            self.cardListView.reloadData()
        }
    }
    
    
}

extension Pet_CardViewController:UICollectionViewDelegate,UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource?.count ?? 0
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let item = collectionView.dequeueReusableCell(withReuseIdentifier: CardList_Item.description(), for: indexPath) as! CardList_Item
        item.model = dataSource?[indexPath.item]
        return item
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = Pet_CardDetailViewController()
        vc.model = dataSource?[indexPath.item]
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

class CardList_Item: UICollectionViewCell {
    
    let coverI = UIImageView.initImgView(img: "")
    let backV = UIView.init(frame: .zero)
    let nameL = UILabel.initV(UIFont.setTypeFace(16,type: .medium), text: "", textColor: .color(hexString: "#666666"))
    let timeL = UILabel.initV(UIFont.setTypeFace(12,type: .medium), text: "", textColor: .color(hexString: "#999999"))
    let descL = UILabel.initV(UIFont.setTypeFace(16,type: .medium), text: "", textColor: titleColor)
    let avarB = UIButton.initV(img: "avarB")

    var model:InfoUtil.room?{
        didSet{
            let arr = self.model?.info2?.components(separatedBy: "@@")
            if let url = URL(string:arr?.first ?? "") {
                self.coverI.kf.setImage(with: url)
            }
            self.nameL.text = model?.info
            self.timeL.text = model?.info4
            if let url = URL(string: self.model?.icon ?? "") {
                self.avarB.kf.setImage(with: url, for: .normal,placeholder: .init(named: "avarB"))
            }
            self.descL.text = model?.info1
        }
    }
            
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.subviews {
            coverI
            backV.subviews {
                avarB
                nameL
                timeL
                descL
            }
        }
        let h = screenHeight - (status_bar_height() + 320 + TabbarHeight)
            
        backV.backgroundColor = mainColor.withAlphaComponent(0.3)
        backV.left(0).right(0).bottom(0).height(120.Fit)
        backV.setAllCorners(corner: 10.Fit)
        backV.isUserInteractionEnabled = true
        
        coverI.left(0).right(0).top(0).height(h - 130.Fit)
        coverI.layer.maskedCorners = [.layerMaxXMinYCorner,.layerMinXMinYCorner]
        coverI.layer.cornerRadius = 100.Fit
        
        avarB.size(45.Fit).left(15.Fit).top(10.Fit)
        avarB.setAllCorners(corner: 22.5.Fit)
        avarB.addAct {
            guard self.model?.createUserId != USEINFOMODEL.id else {
                return
            }
            let vc = Pet_UserinfoViewController()
            vc.userID = self.model?.createUserId
            UIApplication.topViewController()?.navigationController?.pushViewController(vc, animated: true)
        }
        
        nameL.height(20.Fit).top(15.Fit).right(50.Fit)
        nameL.Leading == avarB.Trailing + 15.Fit
        
        timeL.height(15.Fit).right(50.Fit)
        timeL.Leading == avarB.Trailing + 15.Fit
        timeL.Top == nameL.Bottom
        
        descL.right(15.Fit).left(15.Fit).bottom(10.Fit)
        descL.Top == avarB.Bottom + 5.Fit
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


class itemButton: UIButton {
    
    let img = UIImageView.initImgView(img: "")
    let titleL = UILabel.initV(UIFont.setTypeFace(16,type: .medium), text: "", textColor: .color(hexString: "#333333"))
    var nameString = ""{
        didSet{
            titleL.text = nameString
            img.image = .init(named: nameString)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.subviews {
            img
            titleL
        }
        self.addSubview(img)
        self.addSubview(titleL)
        img.snp.makeConstraints { make in
            make.size.equalTo(70.Fit)
            make.top.equalTo(0)
            make.centerX.equalTo(self).offset(0)
        }
        img.setAllCorners(corner: 35.Fit)
        img.layer.borderWidth = 2.Fit
        img.layer.borderColor = mainColor.cgColor
        
        titleL.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.height.equalTo(20.Fit)
            make.top.equalTo(img.snp.bottom).offset(2.Fit)
        }
        titleL.textAlignment = .center
    }
        
    func selectButton(on:Bool) -> Void {
        titleL.textColor = on ? mainColor : .color(hexString: "#333333")
        img.layer.borderWidth = on ? 2.Fit : 0
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


